<template>
  <view class="commission-container">
    
    <!-- 佣金设置卡片 -->
    <view class="commission-card">
      <!-- 省代佣金设置 -->
      <view class="section" style="display: none;">
        <view class="section-title">省代佣金设置</view>
        
        省代基础比例
        <view class="form-item">
          <view class="item-label">省代基础比例</view>
          <view class="item-content">
            <input 
              type="digit" 
              placeholder="0" 
              class="input-field"
              v-model.number="provinceAgent.baseRate"
              @blur="validateProvinceBaseRate"
            />
            <view class="unit">%</view>
          </view>
          <view class="error-tip" v-if="provinceAgent.errors.baseRate">{{ provinceAgent.errors.baseRate }}</view>
        </view>
        
        <!-- 省代额外奖励 -->
        <view class="form-item">
          <view class="item-label">省代额外奖励</view>
          <view class="item-content">
            <input 
              type="digit" 
              placeholder="0" 
              class="input-field"
              v-model.number="provinceAgent.extraReward"
              @blur="validateProvinceExtraReward"
            />
            <view class="unit">元</view>
          </view>
          <view class="error-tip" v-if="provinceAgent.errors.extraReward">{{ provinceAgent.errors.extraReward }}</view>
        </view>
      </view>
      
      <!-- 分隔线 -->
      <view class="divider"></view>
      
      <!-- 分销商佣金设置 -->
      <view class="section">
        <view class="section-title">分销商佣金设置</view>
        
        <!-- 分销基础比例 -->
        <view class="form-item">
          <view class="item-label">分销基础比例</view>
          <view class="item-content">
            <input 
              type="digit" 
              placeholder="0" 
              class="input-field"
              v-model.number="distributor.baseRate"
              @blur="validateDistributorBaseRate"
            />
            <view class="unit">%</view>
          </view>
          <view class="error-tip" v-if="distributor.errors.baseRate">{{ distributor.errors.baseRate }}</view>
        </view>
        
        <!-- 分销商额外奖励 -->
        <view class="form-item">
          <view class="item-label">分销商额外奖励</view>
          <view class="item-content">
            <input 
              type="digit" 
              placeholder="0" 
              class="input-field"
              v-model.number="distributor.extraReward"
              @blur="validateDistributorExtraReward"
            />
            <view class="unit">元</view>
          </view>
          <view class="error-tip" v-if="distributor.errors.extraReward">{{ distributor.errors.extraReward }}</view>
        </view>
      </view>
    </view>
    
    <!-- 保存按钮 -->
    <view class="save-button">
      <button class="btn" @click="saveCommissionSettings">保存设置</button>
    </view>
  </view>
</template>

<script>
	import {
		request
	} from '@/utils/request.js'
export default {
  data() {
    return {
      // 省代佣金设置
      provinceAgent: {
        baseRate: 15, // 基础比例(%)
        extraReward: 50, // 额外奖励(元)
        errors: {
          baseRate: '',
          extraReward: ''
        }
      },
      
      // 分销商佣金设置
      distributor: {
        baseRate: 8, // 基础比例(%)
        extraReward: 20, // 额外奖励(元)
        errors: {
          baseRate: '',
          extraReward: ''
        }
      }
    }
  },
  onLoad(){
	  this.getInfo()
  },
  methods: {
	  
	  getInfo(){
		  request({
		  	url: 'index/bouns-info',
		  	method: 'get',
		  	// data: { username:this.username,password:this.password,openid:uni.getStorageSync("openid")}
		  }).then(res => {
		  	if (res.status == 200) {
				this.provinceAgent.baseRate = res.data.sd_rate
				this.provinceAgent.extraReward = res.data.sd_other
				this.distributor.baseRate = res.data.fx_rate
				this.distributor.extraReward = res.data.fx_other
		  		console.log(res.data)
		  		
		  	} else {
		  		uni.showToast({
		  			title: res.message,
		  			icon: 'none',
		  			duration: 2000
		  		});
		  	}
		  }).catch((err) => {
		  	uni.showToast({
		  		title: err.message,
		  		icon: 'none',
		  		duration: 2000
		  	});
		  })
	  },
	  
	  
	  
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },
    
    // 验证省代基础比例
    validateProvinceBaseRate() {
      if (this.provinceAgent.baseRate < 0 || this.provinceAgent.baseRate > 100) {
        this.provinceAgent.errors.baseRate = '比例范围应为0-100%';
      } else if (!this.provinceAgent.baseRate && this.provinceAgent.baseRate !== 0) {
        this.provinceAgent.errors.baseRate = '请输入省代基础比例';
      } else {
        this.provinceAgent.errors.baseRate = '';
      }
    },
    
    // 验证省代额外奖励
    validateProvinceExtraReward() {
      if (this.provinceAgent.extraReward < 0) {
        this.provinceAgent.errors.extraReward = '额外奖励不能为负数';
      } else {
        this.provinceAgent.errors.extraReward = '';
      }
    },
    
    // 验证分销基础比例
    validateDistributorBaseRate() {
      if (this.distributor.baseRate < 0 || this.distributor.baseRate > 100) {
        this.distributor.errors.baseRate = '比例范围应为0-100%';
      } else if (!this.distributor.baseRate && this.distributor.baseRate !== 0) {
        this.distributor.errors.baseRate = '请输入分销基础比例';
      } else {
        this.distributor.errors.baseRate = '';
      }
    },
    
    // 验证分销商额外奖励
    validateDistributorExtraReward() {
      if (this.distributor.extraReward < 0) {
        this.distributor.errors.extraReward = '额外奖励不能为负数';
      } else {
        this.distributor.errors.extraReward = '';
      }
    },
    
    // 保存佣金设置
    saveCommissionSettings() {
      // 验证所有字段
      this.validateProvinceBaseRate();
      this.validateProvinceExtraReward();
      this.validateDistributorBaseRate();
      this.validateDistributorExtraReward();
      
      // 检查是否有错误
      const hasErrors = 
        this.provinceAgent.errors.baseRate ||
        this.provinceAgent.errors.extraReward ||
        this.distributor.errors.baseRate ||
        this.distributor.errors.extraReward;
      
      if (hasErrors) {
        uni.showToast({
          title: '请检查输入内容',
          icon: 'none'
        });
        return;
      }
      
      // 准备保存的数据
      const commissionData = {
        provinceAgent: this.provinceAgent,
        distributor: this.distributor
      };
      
      // 显示加载提示
      uni.showLoading({
        title: '保存中...',
        mask: true
      });
      
	  request({
	  	url: 'index/bouns-save',
	  	method: 'post',
		data:{
			sd_rate:this.provinceAgent.baseRate,
			sd_other:this.provinceAgent.extraReward,
			fx_rate:this.distributor.baseRate,
			fx_other:this.distributor.extraReward,
		},
	  }).then(res => {
		  uni.hideLoading();
	  	if (res.status == 200) {
	  		console.log(res.data)
			// 隐藏加载提示
			uni.hideLoading();
				console.log(commissionData)
			// 显示成功提示
			uni.showToast({
			  title: '保存成功',
			  icon: 'success'
			});
			
			// 返回上一页
			setTimeout(() => {
			  uni.navigateBack();
			}, 1500);
	  	} else {
			uni.hideLoading();
	  		uni.showToast({
	  			title: res.message,
	  			icon: 'none',
	  			duration: 2000
	  		});
	  	}
	  }).catch((err) => {
	  	uni.showToast({
	  		title: err.message,
	  		icon: 'none',
	  		duration: 2000
	  	});
	  })
      // 模拟API请求
      setTimeout(() => {
      
      }, 1000);
    }
  }
}
</script>

<style lang="scss" scoped>
/* 导入字体图标 */
.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.commission-container {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding-top: 1rpx;
}

/* 顶部导航 */
.page-header {
  height: 100rpx;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  border-bottom: 1rpx solid #eee;
  
  .header-left, .header-right {
    width: 80rpx;
    text-align: center;
  }
  
  .header-title {
    flex: 1;
    text-align: center;
    font-size: 36rpx;
    font-weight: 500;
    color: #333;
  }
  
  .icon-arrow-left {
    font-size: 40rpx;
    color: #666;
  }
}

/* 佣金设置卡片 */
.commission-card {
  background-color: #fff;
  margin: 20rpx;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.section {
  padding: 30rpx 30rpx 20rpx;
  
  .section-title {
    font-size: 32rpx;
    font-weight: 500;
    color: #333;
    margin-bottom: 30rpx;
  }
}

/* 表单项 */
.form-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #eee;
  position: relative;
  
  &:last-child {
    border-bottom: none;
  }
  
  .item-label {
    width: 240rpx;
    font-size: 30rpx;
    color: #666;
  }
  
  .item-content {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    
    .input-field {
      width: 180rpx;
      text-align: right;
      font-size: 30rpx;
      padding: 10rpx 0;
      color: #333;
    }
    
    .unit {
      margin-left: 10rpx;
      font-size: 30rpx;
      color: #999;
    }
  }
  
  .error-tip {
    position: absolute;
    right: 30rpx;
    bottom: 10rpx;
    color: #ff4545;
    font-size: 24rpx;
  }
}

/* 分隔线 */
.divider {
  height: 20rpx;
  background-color: #f5f5f5;
}

/* 保存按钮 */
.save-button {
  padding: 40rpx 30rpx;
  
  .btn {
    width: 100%;
    height: 90rpx;
    line-height: 90rpx;
    background-color: #1677ff;
    color: #fff;
    border-radius: 45rpx;
    font-size: 34rpx;
    text-align: center;
    box-shadow: 0 4rpx 15rpx rgba(22, 119, 255, 0.2);
  }
}
</style>
